//
//  NSString+FKAdd.m
//  FK_Categories
//
//  Created by 云眼科技 on 16/6/13.
//  Copyright © 2016年 CloudEyes. All rights reserved.
//

#import "NSString+FKAdd.h"
#import "NSData+FKAdd.h"


typedef NS_ENUM(NSInteger, FKDirectorType) {
    FKDirectorTypeCache = 0,
    FKDirectorTypeDocument    
};

#define force_inline __inline__ __attribute__((always_inline))

static force_inline NSString *getDirector(FKDirectorType type){
    NSSearchPathDirectory dir;
    switch (type) {
        case FKDirectorTypeCache:
            dir = NSCachesDirectory;
            break;
        case FKDirectorTypeDocument:
            dir = NSDocumentDirectory;
            break;
    }
    return NSSearchPathForDirectoriesInDomains(dir, NSUserDomainMask, YES).firstObject;
}



@implementation NSString (FKAdd)


+(NSString *)getCachePath{
    return getDirector(FKDirectorTypeCache);
}

+(NSString *)getDocumentPath{
    return getDirector(FKDirectorTypeDocument);
}


-(NSString *)FKMd5String{
    return [[self dataUsingEncoding:NSUTF8StringEncoding] md5String];
}

-(CGSize)sizeWithFont:(UIFont *)font size:(CGSize)size lineBreakMode:(NSLineBreakMode)breakMode{
    CGSize result;
    if (!font) {//'font'为'nil'时指定一个默认的值
        font = [UIFont systemFontOfSize:12.f];
    }
    if ([self respondsToSelector:@selector(boundingRectWithSize:options:attributes:context:)]) {
        NSMutableDictionary *attr = [[NSMutableDictionary alloc] init];
        attr[NSFontAttributeName] = font;
        if (breakMode != NSLineBreakByWordWrapping) {
            NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
            style.lineBreakMode = breakMode;
            attr[NSParagraphStyleAttributeName] = style;
        }
        result = [self boundingRectWithSize:size 
                                         options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading 
                                      attributes:attr 
                                         context:nil].size;
        
    }
    return result;
}


-(CGFloat)widthWithFont:(UIFont *)font{
    return [self sizeWithFont:font size:CGSizeMake(MAXFLOAT, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping].width;
}

-(CGFloat)heightWithFont:(UIFont *)font width:(CGFloat)width{
    return [self sizeWithFont:font size:CGSizeMake(width, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping].height;
}


-(NSData *)dataValue{
    return [self dataUsingEncoding:NSUTF8StringEncoding];
}


-(NSString *)base64EncodedString{
    return [[self dataUsingEncoding:NSUTF8StringEncoding] base64EncodingString];
}

+(NSString *)stringWithBase64EncodedString:(NSString *)base64EncodedString{
    NSData *data = [NSData dataWithBase64encodedString:base64EncodedString];
    return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
@end
